<template>
  <div>
    <el-row>
      <el-col :span="12">
        <div class="grid-content bg-purple">
          <el-button type="success" @click="onAddFn">新增</el-button>
          <el-button type="warning">多项删除</el-button>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="grid-content bg-purple-light">
          <el-input v-model="inptVal" placeholder="请输入内容" class="input-with-select">
            <el-button @click="onCloseFn" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </div>
      </el-col>
    </el-row>
    <el-table
      :data="tableList"
      border
      height="500"
      style="width: 100%; margin: 10px auto;"
      @selection-change="handleSelectionChange"
      @sort-change="onSortChange"
    >
      <el-table-column fixed type="selection" width="50"></el-table-column>
      <el-table-column fixed type="index" label="#" width="50"></el-table-column>
      <el-table-column fixed prop="title" label="标题" width="300"></el-table-column>
      <el-table-column prop="type" label="类型" width="200"></el-table-column>
      <el-table-column prop="author" label="作者" width="200"></el-table-column>
      <el-table-column prop="time" label="时间" sortable width="150"></el-table-column>
      <el-table-column prop="date" label="日期" sortable width="200"></el-table-column>
      <el-table-column fixed="right" label="操作" width="300">
        <template slot-scope="scope">
          <el-button type="text" @click="lookData(scope.row)" size="small">查看</el-button>
          <el-button type="text" @click="delFormData(scope.row)" size="small">删除</el-button>
          <el-button type="text" @click="onRecompose(scope.row)" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 抽屉 -->
    <el-drawer
      :title="title"
      :size="'70'"
      :visible.sync="drawer"
      :before-close="handleClose"
      :with-header="true"
      width="70"
    >
      <el-form
        :model="form"
        size="mine"
        style="padding: 0 5%"
        ref="form"
        label-width="100px"
        class="demo-form"
      >
        <el-form-item label="标题" prop="name">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="类别" prop="resource">
          <el-select v-model="form.cid" placeholder="请选择类别">
            <el-option v-for="(v, i) in classifyList" :key="i" :label="v.name" :value="v.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作者" prop="name">
          <el-input :disabled="true" v-model="form.author"></el-input>
        </el-form-item>
        <el-form-item label="用时" prop="desc">
          <el-select v-model="form.time" placeholder="请选择用时">
            <el-option label="一分钟" value="1"></el-option>
            <el-option label="二分钟" value="2"></el-option>
            <el-option label="三分钟" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容">
          <mavonEditor style="max-height: 300px; overflow-y: scroll" v-model="form.content"></mavonEditor>
        </el-form-item>
        <el-form-item>
          <el-button @click="AddDataFn" v-show="isShow" type="primary">{{title}}</el-button>
          <el-button @click="onNoFn">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="0"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import { mavonEditor } from "mavon-editor";
import "mavon-editor/dist/css/index.css";
import { mapActions, mapState, mapMutations } from "vuex";
export default {
  data() {
    return {
      drawer: false,
      form: {
        author: "yabo"
      },
      isShow: true,
      title: "新增",
      UpFormText: "立即创建",
      inptVal: ""
    };
  },
  mounted() {
    this.getMsbdList();
  },
  methods: {
    ...mapActions("msbd", [
      "getMsbdList",
      "getAddList",
      "xgFormList",
      "delMsbdList"
    ]),
    ...mapMutations("msbd", ["SET_CURRENTPAGE", "SET_PAGESIZE"]),
    lookData(v) {
      this.title = "查看";
      this.isShow = false;
      this.form = v;
      this.showDrawer();
    },
    //删除
    delFormData(v) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.delMsbdList(v.id);
          this.getMsbdList();
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    showDrawer() {
      this.drawer = !this.drawer;
    },
    //新增
    onAddFn() {
      this.title = "新增";

      this.showDrawer();
    },
    //搜索确定
    onCloseFn() {
      this.getMsbdList(this.inptVal);
    },
    //取消
    onNoFn() {
      this.form = {
        author: "yabo"
      };
      this.showDrawer();
    },
    // 新增 修改
    AddDataFn() {
      if (this.form.id) {
        this.xgFormList(this.form);
      } else {
        this.getAddList(this.form);
      }
      this.form = {
        author: "yabo"
      };

      this.showDrawer();
      this.getMsbdList();
    },
    //编辑确定
    onRecompose(v) {
      this.form = v;
      this.title = "修改";
      this.showDrawer();
    },

    handleSelectionChange(id) {},
    //排序
    onSortChange(value) {},
    //抽屉关闭
    handleClose(done) {},
    handleSizeChange(val) {
      this.SET_PAGESIZE(val);
      if (this.inptVal) {
        this.getMsbdList(this.inptVal);
      } else {
        this.getMsbdList();
      }
    },
    //当前页数
    handleCurrentChange(val) {
      this.SET_CURRENTPAGE(val);
      if (this.inptVal) {
        this.getMsbdList(this.inptVal);
      } else {
        this.getMsbdList();
      }
    }
  },
  computed: {
    ...mapState("msbd", ["tableList", "classifyList","total"])
  },
  components: {
    mavonEditor
  }
};
</script>

<style scoped>
</style>